﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;
using KPIS.DBM;

namespace KPIS.GERP.GHRMS.BLL
{
    [Serializable]
    public sealed class EmployeeBLL : IEmployeeBLL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        private static readonly IEmployeeDAL dal = DataAccess.CreateEmployees();

        private MasGenderInfo genderInfo = new MasGenderInfo();
        private MasNamePrefixInfo prefixInfo = new MasNamePrefixInfo();
        private MasReligionInfo religionInfo = new MasReligionInfo();
        private MasNationalitiesInfo raceInfo = new MasNationalitiesInfo();
        private MasNationalitiesInfo natInfo = new MasNationalitiesInfo();
        private MasBloodGroupInfo boodInfo = new MasBloodGroupInfo();
        private EmployeeTypeInfo empTypeInfo = new EmployeeTypeInfo();
        private EmployeeStatusInfo esInfo = new EmployeeStatusInfo();
        private ClassInfo classInfo = new ClassInfo();
        private PositionInfo posInfo = new PositionInfo();
        private JobSiteInfo jobsiteInfo = new JobSiteInfo();
        private EmployeeInfo empInfo = new EmployeeInfo();
        private MasMaritalStatusInfo marInfo = new MasMaritalStatusInfo();

        public IList<EmployeeInfo> ListEmployees()
        {
            return dal.ListEmployees(new EmployeeInfo(0));
        }
        public IList<EmployeeInfo> ListEmployees(string Sequence)
        {
            int seq = Sequence.Length > 0 ? Int32.Parse(Sequence) : 0;
            return dal.ListEmployees(new EmployeeInfo(seq));
        }

        public IList<hrm_ps_employees> GetEmployees(string EmpSeq,string EmployeeType, string JobSites)
        {
            hrm_ps_employees h = new hrm_ps_employees();
            h.emp_type_seq = !String.IsNullOrEmpty(EmployeeType) ? Convert.ToInt32(EmployeeType) : 0;
            h.working_job_site_seq = !String.IsNullOrEmpty(JobSites) ? Convert.ToInt32(JobSites) : 0;
            h.emp_seq = !String.IsNullOrEmpty(EmpSeq) ? Convert.ToInt32(EmpSeq) : 0;

            return dal.GetEmployees(h);
        }

        public IList<EmployeeInfo> ListEmployeeByTypes(string EmployeeType, string JobSites, string YearRetire)
        {
            if (EmployeeType.Length > 0)
                empTypeInfo.Sequence = Int32.Parse(EmployeeType);
            if (JobSites.Length > 0)
                jobsiteInfo.Sequence = Int32.Parse(JobSites);
            if (YearRetire.Length > 0)
                empInfo.RetireBudgetYear = Convert.ToInt32(YearRetire);

            empInfo.EmployeeType = empTypeInfo;
            empInfo.WorkingJobSite = jobsiteInfo;
            return dal.ListEmployees(empInfo);
        }
        public IList<EmployeeInfo> ListEmployeeByTypes(string EmployeeType, string JobSites, string YearRetire, string YearRetireTo)
        {
            if (EmployeeType.Length > 0)
                empTypeInfo.Sequence = Int32.Parse(EmployeeType);
            if (JobSites.Length > 0)
                jobsiteInfo.Sequence = Int32.Parse(JobSites);
            if (YearRetire.Length > 0)
                empInfo.RetireBudgetYear = Convert.ToInt32(YearRetire);
            if (YearRetireTo.Length > 0)
                empInfo.RetireBudgetYearEnd = Convert.ToInt32(YearRetireTo);

            empInfo.EmployeeType = empTypeInfo;
            empInfo.WorkingJobSite = jobsiteInfo;
            return dal.ListEmployees(empInfo);
        }
        public DataSet LoadEmployees(Nullable<int> Sequence, string emp_type_seq, bool bActive, string SearchText)
        {
            return dal.LoadEmployees(Sequence, emp_type_seq, (int?)null, (int?)null, bActive, 0, 0, SearchText);
        }
        public DataSet LoadEmployees(Nullable<int> Sequence, string emp_type_seq, bool bActive, string SearchText, string EmpStatusSequence)
        {
            EmpStatusSequence = EmpStatusSequence ?? "";
            if(EmpStatusSequence == "-2")
            {
                bActive = true;
            }
            else if (EmpStatusSequence == "-3")
            {
                bActive = false;
            }
            
            return dal.LoadEmployees(Sequence, emp_type_seq, (int?)null, (int?)null, bActive, 0, 0, SearchText, EmpStatusSequence);
        }
        public DataSet LoadEmployees(Nullable<int> Sequence, string EmpTypeSequence, Nullable<int> JobSiteSequence
            , Nullable<int> WorkingJobSiteSequence, bool bActive, string SearchText)
        {
            return dal.LoadEmployees(Sequence, EmpTypeSequence, JobSiteSequence, WorkingJobSiteSequence, bActive, 0, 0, SearchText);
        }
        public DataSet LoadEmployeeRetire(string EmpTypeSequence, int StartRetire, int EndRetire, bool bActive, string SearchText)
        {
            return dal.LoadEmployees((int?)null, EmpTypeSequence, (int?)null, (int?)null, bActive, StartRetire, EndRetire, SearchText);
        }
        public DataSet LoadEmployeeForWorkingOversea(Nullable<int> Sequence, string EmpTypeSequence
            , Nullable<int> WorkingJobSiteSequence, int DurationOversea, Nullable<DateTime> StartDateNew
            , string SearchText)
        {
            return dal.LoadEmployeeForWorkingOversea(Sequence, EmpTypeSequence, WorkingJobSiteSequence, DurationOversea
                , StartDateNew, SearchText);
        }
        public DataSet LoadEmployeeForApproveCommands()
        {
            return dal.LoadEmployeeForApproveCommands();
        }

        public DataSet Detail()
        {
            return dal.Detail();
        }

        public DataSet LoadToExcel(string EmployeeType, string JobSite, string WorkingJobSite
            , string PositionType, string Class, string FieldPositon, string EduDegree
            , string EduQualification, string EduMajor, string EduInstitutes
            , Nullable<double> WorkingDayCount, Nullable<DateTime> WorkingDayCountDate
            , Nullable<double> AgeCount, Nullable<DateTime> AgeCountDate
            , Nullable<int> KeepClass, Nullable<int> KeepClassCount, bool bActive)
        {
            return dal.LoadToExcel(EmployeeType, JobSite, WorkingJobSite
            , PositionType, Class, FieldPositon, EduDegree, EduQualification, EduMajor, EduInstitutes
            , WorkingDayCount, WorkingDayCountDate, AgeCount, AgeCountDate, KeepClass, KeepClassCount, bActive);
        }

        public string Insert(string FirstName, string LastName, string FirstNameEng, string LastNameEng
            , string NickName, string Citizen, string TaxPayer, string Social, string Official, string NamePrefix
            , string Gender, string Religion, string RaceNationalities, string Nationalities
            , string BloodGroup, Nullable<DateTime> BirthDate, Nullable<DateTime> DeathDate
            , string EmployeeType, string Class, Nullable<int> SalaryLevel, Nullable<double> Salary
            , string Position, string WorkingJobSite, Nullable<DateTime> AdmittedDate
            , Nullable<DateTime> WorkStartDate, Nullable<DateTime> QuitDate, string Status, bool FundMember 
            , Nullable<DateTime> ProbationEndDate, Nullable<DateTime> LatestPositionDate
            , Nullable<DateTime> LatestClassDate, Nullable<DateTime> LatestWorkingJobSiteDate, int CreatedBy
            , string MaritalStatus, string CodeScan, int? PositionType, string imageUnique = "", string ExecPosition = "")
        {
            genderInfo.Sequence = Gender.Length > 0 ? Int32.Parse(Gender) : 0;
            prefixInfo.Sequence = NamePrefix.Length > 0 ? Int32.Parse(NamePrefix) : 0;
            religionInfo.Sequence = Religion.Length > 0 ? Int32.Parse(Religion) : 0;
            raceInfo.Sequence = RaceNationalities.Length > 0 ? Int32.Parse(RaceNationalities) : 0;
            natInfo.Sequence = Nationalities.Length > 0 ? Int32.Parse(Nationalities) : 0;
            boodInfo.Sequence = BloodGroup.Length > 0 ? Int32.Parse(BloodGroup) : 0;
            empTypeInfo.Sequence = EmployeeType.Length > 0 ? Int32.Parse(EmployeeType) : 0;
            esInfo.Sequence = Status.Length > 0 ? Int32.Parse(Status) : 0; ;
            classInfo.Sequence = Class.Length > 0 ? Int32.Parse(Class) : 0;
            posInfo.Sequence = Position.Length > 0 ? Int32.Parse(Position) : 0;
            posInfo.Executive = !String.IsNullOrEmpty(ExecPosition) ? new ExecutiveInfo { Sequence = Convert.ToInt32(ExecPosition) } : null;
            if (posInfo.Sequence != 0 && PositionType.HasValue)
            {
                posInfo.PositionType = new PositionTypeInfo((int) PositionType, "", "", "", "", null, "", null, null);
            }
            jobsiteInfo.Sequence = WorkingJobSite.Length > 0 ? Int32.Parse(WorkingJobSite) : 0;
            marInfo.Sequence = MaritalStatus.Length > 0 ? Int32.Parse(MaritalStatus) : 0;

            PersonInfo personInfo = new PersonInfo(FirstName, LastName, FirstNameEng, LastNameEng, NickName, Citizen,
                                                   TaxPayer
                                                   , Social, Official, prefixInfo, genderInfo, religionInfo, raceInfo,
                                                   natInfo, BirthDate, boodInfo, DeathDate);

	        personInfo.MaritalStatus = marInfo;  

            try
            {
                return dal.Insert(new EmployeeInfo(0, personInfo, empTypeInfo, classInfo, SalaryLevel, Salary, posInfo
                                                   , jobsiteInfo, AdmittedDate, WorkStartDate, QuitDate, esInfo, 0,
                                                   ProbationEndDate
                                                   , LatestPositionDate, LatestClassDate, LatestWorkingJobSiteDate,
                                                   FundMember, "U", CreatedBy, 0)
                                      {
                                          CodeScan = CodeScan,
                                          ImageUnique = imageUnique
                                      });
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        public void Update(int Sequence, string FirstName, string LastName, string FirstNameEng, string LastNameEng
            , string NickName, string Citizen, string TaxPayer, string Social, string Official, string NamePrefix
            , string Gender, string Religion, string RaceNationalities, string Nationalities
            , string BloodGroup, Nullable<DateTime> BirthDate, Nullable<DateTime> DeathDate
            , string EmployeeType, string Class, Nullable<int> SalaryLevel, Nullable<double> Salary
            , string Position, string WorkingJobSite, Nullable<DateTime> AdmittedDate, Nullable<DateTime> WorkStartDate
            , Nullable<DateTime> QuitDate, string Status, bool FundMember, string MaritalStatus
            , Nullable<DateTime> ProbationEndDate, Nullable<DateTime> LatestPositionDate
            , Nullable<DateTime> LatestClassDate, Nullable<DateTime> LatestWorkingJobSiteDate, string CodeScan, int UpdatedBy
            , int? PositionType, string imageUnique = "", string ExecPosition = "")
        {
            genderInfo.Sequence = Gender.Length > 0 ? Int32.Parse(Gender) : 0;
            prefixInfo.Sequence = NamePrefix.Length > 0 ? Int32.Parse(NamePrefix) : 0;
            religionInfo.Sequence = Religion.Length > 0 ? Int32.Parse(Religion) : 0;
            raceInfo.Sequence = RaceNationalities.Length > 0 ? Int32.Parse(RaceNationalities) : 0;
            natInfo.Sequence = Nationalities.Length > 0 ? Int32.Parse(Nationalities) : 0;
            boodInfo.Sequence = BloodGroup.Length > 0 ? Int32.Parse(BloodGroup) : 0;
            empTypeInfo.Sequence = EmployeeType.Length > 0 ? Int32.Parse(EmployeeType) : 0;
            esInfo.Sequence = Status.Length > 0 ? Int32.Parse(Status) : 0; ;
            classInfo.Sequence = Class.Length > 0 ? Int32.Parse(Class) : 0;
            posInfo.Sequence = Position.Length > 0 ? Int32.Parse(Position) : 0;
            jobsiteInfo.Sequence = WorkingJobSite.Length > 0 ? Int32.Parse(WorkingJobSite) : 0;
            marInfo.Sequence = MaritalStatus.Length > 0 ? Int32.Parse(MaritalStatus) : 0;
            posInfo.Executive = !String.IsNullOrEmpty(ExecPosition) ? new ExecutiveInfo { Sequence = Convert.ToInt32(ExecPosition) } : null;
			//if (posInfo.Sequence != 0 && PositionType.HasValue)
			//{
			//	posInfo.PositionType = new PositionTypeInfo((int) PositionType, "", "", "", "", null, "", null, null);
			//}
			if(posInfo.Sequence != 0 && PositionType.HasValue)
			{
				posInfo.PosTypeSeq = PositionType;
			}

            PersonInfo personInfo = new PersonInfo(FirstName, LastName, FirstNameEng, LastNameEng, NickName, Citizen, TaxPayer
                , Social, Official, prefixInfo, genderInfo, religionInfo, raceInfo, natInfo, BirthDate, boodInfo, DeathDate);
            personInfo.MaritalStatus = marInfo;

            EmployeeInfo empInfo = new EmployeeInfo(Sequence, personInfo, empTypeInfo, classInfo, SalaryLevel,
                                                    Salary, posInfo
                                                    , jobsiteInfo, AdmittedDate, WorkStartDate, QuitDate, esInfo, 0,
                                                    ProbationEndDate
                                                    , LatestPositionDate, LatestClassDate, LatestWorkingJobSiteDate,
                                                    FundMember, "", 0, UpdatedBy)
                                        {
                                            ImageUnique = imageUnique,
                                            CodeScan = CodeScan
                                        };

            dal.Update(empInfo);
        }

        public void UpdateSingleField(int Sequence, string fieldName, object fieldValue, int updateBy)
        {
           dal.UpdateSingleField(Sequence,fieldName,fieldValue,updateBy);
        }

        public void UpdatePersonal(int Sequence, string FirstName, string LastName, string FirstNameEng, string LastNameEng
            , string NickName, string Citizen, string TaxPayer, string Social, string Official, string NamePrefix
            , string Gender, string Religion, string RaceNationalities, string Nationalities
            , string BloodGroup, Nullable<DateTime> BirthDate, Nullable<DateTime> DeathDate, int UpdatedBy)
        {
            genderInfo.Sequence = Gender.Length > 0 ? Int32.Parse(Gender) : 0;
            prefixInfo.Sequence = NamePrefix.Length > 0 ? Int32.Parse(NamePrefix) : 0;
            religionInfo.Sequence = Religion.Length > 0 ? Int32.Parse(Religion) : 0;
            raceInfo.Sequence = RaceNationalities.Length > 0 ? Int32.Parse(RaceNationalities) : 0;
            natInfo.Sequence = Nationalities.Length > 0 ? Int32.Parse(Nationalities) : 0;
            boodInfo.Sequence = BloodGroup.Length > 0 ? Int32.Parse(BloodGroup) : 0;

            PersonInfo personInfo = new PersonInfo(FirstName, LastName, FirstNameEng, LastNameEng, NickName, Citizen, TaxPayer
                , Social, Official, prefixInfo, genderInfo, religionInfo, raceInfo, natInfo, BirthDate, boodInfo, DeathDate);
            try
            {
                dal.UpdatePersonal(new EmployeeInfo(Sequence, personInfo, empTypeInfo, classInfo, 0, 0, posInfo
                    , jobsiteInfo, (DateTime?)null, (DateTime?)null, (DateTime?)null, esInfo, 0, (DateTime?)null
                    , (DateTime?)null, (DateTime?)null, (DateTime?)null, false, "", 0, UpdatedBy));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                dal.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UploadFile(string DestPath, string ImageName)
        {
            ImageInfo info = new ImageInfo();

            try
            {
                info.Dest_path = DestPath;

                dal.UploadFile(info, ImageName);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UploadFileToServer(string DestPath, string ImageName)
        {
            ImageInfo info = new ImageInfo();

            try
            {
                info.Dest_path = DestPath;

                dal.UploadFileToServer(info, ImageName);
            }
            catch
            {
                throw new Exception("");
            }
        }

        public string GetGenderSeq(string NamePrefixSeq)
        {
            return dal.GetGenderSeq(NamePrefixSeq);
        }

    }
}
